package com.controller;

import com.entity.BUser;
import com.entity.table.BUserTableDef;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.BUserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin")
public class UserController {
    @Autowired
    BUserService bUserService;
    @GetMapping("/login") // 显示登录页面
    public String toLoginPage() {
        return "back/login";
    }
    @PostMapping("/toLogin") // 提交登录，登录成功跳转至后台首页
    public String login(BUser bUser, HttpSession httpSession, Model model) {
//        BUser bUser = bUserMapper.userLogin(admin);
        QueryWrapper query = QueryWrapper.create()
                .where(BUserTableDef.B_USER.USERNAME.eq(bUser.getUsername()))
                .where(BUserTableDef.B_USER.PASSWORD.eq(bUser.getPassword()));
        BUser bUser1 = bUserService.getOne(query);

        if (bUser1!= null) {
            httpSession.setAttribute("admin", bUser); // 将用户对象添加到 Session
            model.addAttribute("admin", bUser);
            return "back/index";
        } else {
            model.addAttribute("msg", "账号或密码错误，请重新输入！");
            return "back/login";

        }
    }
}
